#![allow(unused_variables)]
use sqlx::Error;
use super::CountNum;
use super::PermissionBean;
use super::PermissionQueryRequest;
use super::Table;

impl<'c> Table<'c, PermissionBean> {
pub async fn activate_permission(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_permissions set active = true where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn add_permission(&self,permission:PermissionBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"insert into cs_permissions ( perm_name,active) values (?,?)"#
		)
		.bind(permission.perm_name)
		.bind(permission.active)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn count_active_permission_records(&self) ->Result<CountNum, Error> {
		let result = sqlx::query_as(
		r#"select count(*) as countNum from cs_permissions where active = true"#
		)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn count_search_permissions_by_fields_records(&self,permission_query_request:PermissionQueryRequest) ->Result<CountNum, Error> {
		let mut query = r#"select count(*) as countNum from cs_permissions where 1=1 "#.to_string();
		let perm_name = permission_query_request.perm_name.unwrap_or_default();
		if !perm_name.is_empty() {
			let query_perm_name  = format!("{}{}{}"," and perm_name like '%",perm_name,"%' ");
			query = format!("{} {}", query, query_perm_name);
		}
		let active = permission_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let result = sqlx::query_as(&query)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn delete_permission(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"delete from cs_permissions where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_permission_by_id(&self,id:i64) ->Result<PermissionBean, Error> {
	let result = sqlx::query_as(
	r#"select id,perm_name,active from cs_permissions where id = ? "#
		)
		.bind(id)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn find_permission_by_perm_name(&self,perm_name:String) ->Result<PermissionBean, Error> {
	let result = sqlx::query_as(
	r#"select id,perm_name,active from cs_permissions where perm_name = ?;"#
		)
		.bind(perm_name)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_active_permissions(&self) ->Result<Vec<PermissionBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,perm_name,active from cs_permissions where active = true order by perm_name asc"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_all_permissions(&self) ->Result<Vec<PermissionBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,perm_name,active from cs_permissions"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_permissions_by_fields(&self,permission_query_request:PermissionQueryRequest) ->Result<Vec<PermissionBean>, Error> {
		let mut query = r#"select id,perm_name,active from cs_permissions where 1=1 "#.to_string();
		let perm_name = permission_query_request.perm_name.unwrap_or_default();
		if !perm_name.is_empty() {
			let query_perm_name  = format!("{}{}{}"," and perm_name like '%",perm_name,"%' ");
			query = format!("{} {}", query, query_perm_name);
		}
		let active = permission_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_permissions_by_fields_by_page(&self,permission_query_request:PermissionQueryRequest) ->Result<Vec<PermissionBean>, Error> {
		let mut query = r#"select id,perm_name,active from cs_permissions where 1=1 "#.to_string();
		let perm_name = permission_query_request.perm_name.unwrap_or_default();
		if !perm_name.is_empty() {
			let query_perm_name  = format!("{}{}{}"," and perm_name like '%",perm_name,"%' ");
			query = format!("{} {}", query, query_perm_name);
		}
		let active = permission_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let pagenum =  permission_query_request.page.unwrap_or_default();
		let pagesize =  permission_query_request.rows.unwrap_or_default();
		let start = (pagenum - 1) * pagesize;
		let limit = pagesize;
		query = format!("{} limit {} offset {} ", query, limit, start);
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn soft_delete_permission(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_permissions set active = false where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn toggle_permission(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_permissions set active = not active where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn update_permission(&self,permission:PermissionBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_permissions set perm_name = ? ,active = ? where id = ? "#
		)
		.bind(permission.perm_name)
		.bind(permission.active)
		.bind(permission.id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_permission_indexed_name(&self,short_name:String) ->Result<Vec<PermissionBean>, Error> {
	let result = sqlx::query_as(
	r#"select id,perm_name,active from cs_permissions where perm_name like concat(? ,'%') "#
	)
	.bind(short_name)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
		return result;
}

}
